<?php

	include_once("cmsConfig.php");
	include_once(CMS_HOME_DIR . "/lib/cmsBlobHelper.php");

	/**
	 * this provides a preview of a imageblock.. it will always read from the upload blobs.
	 * we dont use this for the actual newsletter which needs to be saved.
	 */

//--

	function do404(){
		header("HTTP/1.1 404 Not Found",true);
		header("Content-Type: text/plain",true);
		echo "what you are looking for,\nis no more.\nhttp error 404";
	}

	//this assumes the session is already there and running
	//session_start();

	$authInfo = extractObjectWithKeyFromArray('authInfo', $_SESSION, null);
	//we only do this is we have no session
	if ( is_null($authInfo)) {
		die("NOT LOGGED INTO CMS");
	//	$authInfo = setupPublicUser();
	}

	$imageContentBlockId = extractStringWithKeyFromArray('imageContentBlockId',$_GET, null);
	if (is_null($imageContentBlockId) || strlen($imageContentBlockId) != 8) {//
		do404();
		echo "\n\nAdditionally:\n\t No imageContentBlockId was specified";
		return;
	}

	// first we check to see if we have a persisted imageContentBlock in the db.. we want to use this if we can.
	// otherwise we have to then trawl through the session and look for unsaved items.
	// if we are coming out of the db .. then we can basically fall back to blob.php for the iamge.. otherwise we need to do it the
	// hard way..
	$imageBlock = new CmsImageContentBlock();
	$loaded = $imageBlock->load($imageContentBlockId, $authInfo, $db);

	if (!$loaded) {
		//this is where things get icky as we now need to try and get things from the session..
		// so first we get the mailoutobject if we can.. there should only be one of these in the session... called mailout
		$cmsKvcO = extractObjectWithKeyOfClassFromArray($_SESSION['cmsKvcName'],$_SESSION['cmsKvcClass'], $_SESSION, null);
		if (is_null($cmsKvcO) ) {
			do404();
			echo "\n\nAdditionally:\n\t No cms KCV object was was found in the user session";
			return;
		}

		//
		// look for the image block.. to do this.. we need to loop through the content blocks and check.
		// we could look at the file system directly.. but if the item has not been saved, it only belongs to the user
		// who owns the session.. this just provides an extra layer of sanity checking.. so that we dont look at other peoples shit.
		//
		$imageBlock = null;
		foreach ($cmsKvcO->valueForKey('contentBlocks') as $cb) {
			// now we check the images.. we loop through the images.. and check for a matching id..
			$imageBlock = $cb->imageContentBlockForId($imageContentBlockId);
			if (!is_null($imageBlock)){
				break;
			}
		}
	}

	//
	//here we now need to decide how we are going to find the file.
	if (is_null($imageBlock)) {
		do404();
		echo "\n\nAdditionally:\n\t No image block was found in the user session";
		return;
	}

	//file_exists($imageBlock)
	$imageDetails = $imageBlock->valueForKey('image');
	if (is_null($imageDetails) || !is_array($imageDetails)){
		do404();
		echo "\n\nAdditionally:\n\t No image data in the image block";
		return;
	}

	$blob = null;
	$byteSize = 0;
	$responseHeaders = array();
	if (key_exists('file',$imageDetails) && !is_null($imageDetails['file']) && file_exists($imageDetails['file'])) {
		$blob = file_get_contents($imageDetails['file']);

		if (!is_null($blob)){

			//try and get a resized copy
			$fileTypeCode = extractStringWithKeyFromArray('fileTypeCode', $_REQUEST, $imageBlock->valueForKey('fileType'));
			$options = getImageDescriptor('imageBlock', 11288, $fileTypeCode  ,$db);

			if ($fileTypeCode != 1)
				$blob = processImageData($blob, $options);

			$byteSize = byteSize($blob);
			$responseHeaders["Content-Type"] = $imageDetails['type'];
			$responseHeaders["Content-Transfer-Encoding"] = "binary";
			$responseHeaders["Content-length"] = $byteSize;

		}
	}

	//
	if (!is_null($blob)  ){//&& $byteSize > 0){

		sendCacheHeaders();
		streamBlobDataWithHeaders($blob, $responseHeaders);

	} else {
	//	//otherwise
		do404();
		if (is_array($responseHeaders)) {
			echo "Additonal Information:\n";
			foreach ($responseHeaders as $header) {
				echo($header);
			}
		}
		if (!is_null($imageBlock)){
			echo "\n";
			echo "\n";
			print_r($imageBlock);
		}
		echo "\n";
	}
